package com.threeman.maper.map;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.threeman.maper.MainActivity;

public class CustomMapView extends MapView
{
	private static String TAG = "CustomMapView";

	public static int MAP_NORMAL = 0;

	public static int MAP_SATELLIE = 1;

	private Context mContext;

	private MapController mController;

	// LISTENERS

	OnTouchFreezeTimeChangeListener onTouchFreezeTimeChangeListener = null;

	// TOUCH ANIMATION FREEZE

	private boolean mAnimate = true;

	private Timer mTimerTouch;

	private int mTouchFreezeTime = 10;

	private int mTimerTouchTicks = 0;


	public void setOnTouchFreezeTimeChangeListener(OnTouchFreezeTimeChangeListener listener)
	{
		onTouchFreezeTimeChangeListener = listener;
	}


	public boolean isAnimationAvailable()
	{
		return mAnimate;
	}


	public int getTouchFreezeTime()
	{
		return mTouchFreezeTime;
	}


	public void setTouchFreezeTime(int touchStopTime)
	{
		if (touchStopTime > 1)
			this.mTouchFreezeTime = touchStopTime;
	}


	public int getCurrentTouchFreezeTime()
	{
		return mTouchFreezeTime - mTimerTouchTicks;
	}


	public CustomMapView(Context context, String apiKey)
	{
		super(context, apiKey);
		mContext = context;
		initialize();
	}


	public CustomMapView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		mContext = context;
		initialize();
	}


	public CustomMapView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		mContext = context;
		initialize();
	}


	private void initialize()
	{
		setType(CustomMapView.MAP_SATELLIE);
	}


	public void setType(int type)
	{
		switch ( type )
		{
			case 0 :
				this.setSatellite(false);
				return;
			case 1 :
				this.setSatellite(true);
				return;
		}
		return;
	}


	@Override
	public boolean onTouchEvent(MotionEvent ev)
	{
		boolean ret = super.onTouchEvent(ev);

		if (ev.getAction() == MotionEvent.ACTION_DOWN)
		{
			if (mTimerTouch == null)
			{
				Log.i(MainActivity.APPTAG, TAG + " : START TOUCH TIMER ");

				mTimerTouch = new Timer("TimerTouch");
				mTimerTouch.schedule(new AnimateTask(), new Date(), 1000);
			} else
				mTimerTouchTicks = 0;
		}

		return ret;
	}

	protected class AnimateTask extends TimerTask
	{

		private Handler mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg)
			{
				switch ( msg.what )
				{
					case 0 :
						if (onTouchFreezeTimeChangeListener != null)
						{
							onTouchFreezeTimeChangeListener.onTouchFreezeTimeChanged(mTouchFreezeTime - mTimerTouchTicks);
						}
						return;
				}
			}

		};


		@Override
		public void run()
		{
			// Log.i(MainActivity.APPTAG, TAG + " : TIMER TASK EXECUTION (" + (mTouchStopTime - mTimerTouchTicks) + ")");

			mHandler.sendEmptyMessage(0);

			mAnimate = false;

			if (mTimerTouchTicks == mTouchFreezeTime)
			{
				Log.i(MainActivity.APPTAG, TAG + " : STOP TOUCH TIMER ");

				mTimerTouchTicks = 0;
				mTimerTouch.cancel();
				mTimerTouch = null;
				mAnimate = true;
				return;
			}

			mTimerTouchTicks++;
		}

	}

	public interface OnTouchFreezeTimeChangeListener
	{
		public abstract void onTouchFreezeTimeChanged(int currentTime);

	}
}
